# -*- coding: utf-8 -*-
"""
Module Description:活动相关接口
Date: 2019/7/18
Author:Chen Murong
"""
from pyshare.framework.error_handle import GameException
from pyshare.const.lang.tips_info import TipsInfo
from pyshare.utils.time_utils import getCT
from pyshare.dal.gameconfig.game_env import get_config_env_set
from pyshare.dal.repository_impl import RoleRepository


def get_rank_award(rank, range_list, award_list, raise_error=True):
    """获取奖励"""
    award_index = get_award_index(rank, range_list)
    if award_index > -1:
        # 可领取
        return award_list[award_index]
    if raise_error:
        """未达到领取奖励"""
        raise GameException(TipsInfo.ActivityKeys.notAchieveAward)
    return None


def get_award_index(rank, range_list):
    """获取奖励索引"""
    award_index = -1
    for index, r in enumerate(range_list):
        if r[0] <= rank <= r[1]:
            award_index = index
    return award_index


def is_has_rank_award(rank, range_list):
    """是否有排行榜奖励"""
    return 0 < rank <= range_list[-1][-1]


def change_type_of_nest_list_element(nest_list, change_index, change_type):
    """
    背景：数据组需要榜单埋点中的server_id是整数类型，遂写下了该函数
    改变双层列表中某个元素的类型: int 和 str 互转

    :param nest_list: [[1,1,"1"]]
    :param change_index: 需要改变元素类型的元素索引
    :param change_type: 需要改变成什么类型 int 或 str
    :return: [[1,1,1]]
    """
    try:
        if not isinstance(nest_list, list):
            return nest_list
        for i in nest_list:
            if not isinstance(i, list):
                return nest_list
        if change_type not in [int, str]:
            return nest_list

        for inner_list in nest_list:
            for index in range(0, len(inner_list)):
                if index == change_index:
                    inner_list[index] = change_type(inner_list[index])
        return nest_list
    except Exception:
        return nest_list


def cross_activity_all_achieve_award_check(rid):
    """
    跨服活动一键领取条件校验
    """
    role = RoleRepository.get_by_rid(rid, "RoleLv", "VipLv")

    # 君王等级限制
    role_lv_limit = get_config_env_set("CrossActivityAllAchieveAwardRoleLvLimit")
    # vip等级限制
    vip_lv_limit = get_config_env_set("CrossActivityAllAchieveAwardVipLvLimit")

    if (role.role_lv >= role_lv_limit) or (role.vip_lv >= vip_lv_limit):
        return True
    else:
        return False
